home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / linux / tools / gtar10.lha / port.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-10  |  26.9 KB  |  1,329 lines

  1. /* Supporting routines which may sometimes be missing.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * @(#)port.c 1.15    87/11/05    by John Gilmore, 1986
  22.  *
  23.  * These are routines not available in all environments.
  24.  *
  25.  * I know this introduces an extra level of subroutine calls and is
  26.  * slightly slower.  Frankly, my dear, I don't give a damn.  Let the
  27.  * Missed-Em Vee losers suffer a little.  This software is proud to
  28.  * have been written on a BSD system.
  29.  */
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <signal.h>
  34. #include <errno.h>
  35.  
  36. #if    defined(__MSDOS__) || defined(USG)
  37. #include <fcntl.h>
  38. #else
  39. #include <sys/file.h>
  40. #endif
  41.  
  42. #include "tar.h"
  43. #include "port.h"
  44.  
  45. #ifdef USG
  46. #include <string.h>
  47. #else
  48. extern size_t strlen();
  49. #endif
  50.  
  51. extern long baserec;
  52. /*
  53.  * Some people (e.g. V7) don't have a #define for these.
  54.  */
  55. #ifndef    O_BINARY
  56. #define    O_BINARY    0
  57. #endif
  58. #ifndef    O_RDONLY
  59. #define    O_RDONLY    0
  60. #endif
  61. #ifndef NULL
  62. #define NULL 0
  63. #endif
  64.  
  65. /* JF: modified so all configuration information can appear here, instead of
  66.    being scattered through the file.  Add all the machine-dependent #ifdefs
  67.    here */
  68. #undef WANT_DUMB_GET_DATE/* WANT_DUMB_GET_DATE --> get_date() */
  69. #undef WANT_VALLOC    /* WANT_VALLOC --> valloc() */
  70. #undef WANT_MKDIR    /* WANT_MKDIR --> mkdir() rmdir() */
  71. #undef WANT_STRING    /* WANT_STRING --> index() bcopy() bzero() bcmp() */
  72. #undef WANT_BZERO    /* WANT_BZERO --> bzero() bcmp() execlp() */
  73.             /* EMUL_OPEN3 --> open3() */
  74. #undef WANT_MKNOD    /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  75. #undef WANT_UTILS    /* WANT_UTILS --> panic() ck_*() *_buffer()
  76.                merge_sort() quote_copy_string() un_quote_string() */
  77. #undef WANT_CK_PIPE    /* WANT_CK_PIPE --> ck_pipe() */
  78. #undef WANT_GETWD    /* WANT_GETWD --> getwd() */
  79. #undef WANT_STRSTR    /* WANT_STRSTR --> strstr() */
  80. #undef WANT_FTRUNCATE    /* WANT_FTRUNCATE --> frtruncate() */
  81.  
  82. /* Define only ONE of these four . . . */
  83. /* #undef DOPRNT_MSG    /* Define this one if you have _doprnt() and
  84.                no varargs support */
  85. /* #undef VARARGS_MSG    /* Define this one if you have varargs.h and
  86.                vfprintf() */
  87. /* #undef STDC_MSG     /* Define this one if you are using ANSI C and
  88.                and have vfprintf() */
  89. /* #undef LOSING_MSG    /* Define this one if you don't have any of the
  90.                above */
  91. #ifdef USG
  92. #define WANT_STRING
  93. #define WANT_VALLOC
  94.  
  95. #if defined(sgi) && defined(mips)
  96. #define WANT_GETWD
  97. #endif
  98.  
  99. #if defined(i386)
  100. #define WANT_FTRUNCATE
  101. #endif
  102.  
  103. #endif
  104.  
  105. #ifdef hpux
  106. #define WANT_VALLOC
  107. #endif
  108.  
  109. #ifdef MINIX
  110. #define WANT_BZERO
  111. #endif
  112.  
  113. #ifdef __MSDOS__
  114. char TTY_NAME[] = "con";
  115.  
  116. #define WANT_STRING
  117. #define WANT_MKNOD
  118. #define WANT_UTILS
  119. #define WANT_VALLOC
  120.  
  121. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  122. #ifdef __STDC__
  123. #define STDC_MSG
  124. #else
  125. #define LOSING_MSG
  126. #endif
  127. #endif
  128.  
  129. #else /* not MSDOS */
  130. #ifdef amigados
  131. char TTY_NAME[] = "*";
  132. #else
  133. char TTY_NAME[] ="/dev/tty";
  134. #endif
  135.  
  136. #define WANT_UTILS
  137. #define WANT_CK_PIPE
  138. #ifndef HAVE_STRSTR
  139. #define WANT_STRSTR
  140. #endif
  141.  
  142. #if (!defined(STDC_MSG) && !defined(DOPRNT_MSG) && !defined(VARARGS_MSG) && !defined(LOSING_MSG))
  143. #ifdef BSD42
  144. /* BSD systems should do this even if __STDC__, because
  145.    we might be using an ANSI compiler without an ANSI library. (sigh) */
  146. #ifdef sparc
  147. #define LOSING_MSG
  148. #else
  149. #define DOPRNT_MSG
  150. #endif
  151. #else /* not BSD */
  152. #ifdef __STDC__
  153. #define STDC_MSG
  154. #else /* not ANSI C */
  155. #define LOSING_MSG
  156. #endif /* not ANSI C */
  157. #endif /* not BSD */
  158. #endif /* Need to define some form of _MSG */
  159. #endif /* not MSDOS */
  160.  
  161. /* End of system-dependent #ifdefs */
  162.  
  163. #ifdef WANT_DUMB_GET_DATE
  164. /* JF a get_date() routine takes a date/time/etc and turns it into a time_t */
  165. /* This one is a quick hack I wrote in about five minutes to see if the N
  166.    option works.  Someone should replace it with one that works */
  167.  
  168. /* This get_date takes an arg of the form mm/dd/yyyy hh:mm:ss and turns it
  169.    into a time_t .  Its not well tested or anything. . .  */
  170. /* In general, you should use the get_date() supplied in getdate.y */
  171.  
  172. #define OFF_FROM GMT 18000        /* Change for your time zone! */
  173.  
  174. time_t
  175. get_date(str)
  176. char *str;
  177. {
  178.     int month,day,year,hour,minute,second;
  179.     time_t ret;
  180.     int    n;
  181.  
  182. #define SECS_PER_YEAR (365L*SECS_PER_DAY)
  183. #define SECS_PER_LEAP_YEAR (366L*SECS_PER_DAY)
  184.  
  185. #define SECS_PER_DAY (24L*60*60)
  186.     static int days_per_month[2][12] = {
  187.         31,28,31,30,31,30,31,31,30,31,30,31,
  188.         31,29,31,30,31,30,31,31,30,31,30,31
  189.     };
  190.  
  191.     static int days_per_year[2]={365,366};
  192.  
  193.     month=day=year=hour=minute=second=0;
  194.     n=sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second);
  195.     if(n<3)
  196.         return 0;
  197.     if(year<100)
  198.         year+=1900;
  199.     if(year<1970)
  200.         return 0;
  201.     ret=0;
  202.  
  203.     ret+=OFF_FROM_GMT;
  204.  
  205.     for(n=1970;n<year;n++)
  206.         if(n%4==0 && n%400!=0)
  207.             ret+=SECS_PER_LEAP_YEAR;
  208.         else
  209.             ret+=SECS_PER_YEAR;
  210.  
  211.     month--;
  212.     for(n=0;n<month;n++) {
  213.         if(year%4==0 && year%400!=0)
  214.             ret+=SECS_PER_DAY*days_per_month[1][n];
  215.         else
  216.             ret+=SECS_PER_DAY*days_per_month[0][n];
  217.     }
  218.     ret+=SECS_PER_DAY*(day-1);
  219.     ret+=second+minute*60+hour*60*60;
  220.     return ret;
  221. }
  222. #endif
  223.  
  224. #ifdef WANT_VALLOC
  225. /*
  226.  * valloc() does a malloc() on a page boundary.  On some systems,
  227.  * this can make large block I/O more efficient.
  228.  */
  229. char *
  230. valloc (size)
  231.     unsigned size;
  232. {
  233.     extern char *malloc ();
  234.     return (malloc (size));
  235. }
  236. #endif
  237. /*
  238.  *                NMKDIR.C
  239.  *
  240.  * Written by Robert Rother, Mariah Corporation, August 1985. 
  241.  *
  242.  * I wrote this out of shear disgust with myself because I couldn't
  243.  * figure out how to do this in /bin/sh.
  244.  *
  245.  * If you want it, it's yours.  All I ask in return is that if you
  246.  * figure out how to do this in a Bourne Shell script you send me
  247.  * a copy.
  248.  *                    sdcsvax!rmr or rmr@uscd
  249. *
  250. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  251. * subroutine.    11Mar86; hoptoad!gnu
  252. *
  253. * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  254. * subroutine didn't return EEXIST.  It does now.
  255. */
  256.  
  257. /*
  258.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  259.  */
  260. #ifdef WANT_MKDIR
  261. int
  262. mkdir(dpath, dmode)
  263.     char *dpath;
  264.     int dmode;
  265. {
  266.     int cpid, status;
  267.     struct stat statbuf;
  268.     extern int errno;
  269.  
  270.     if (stat(dpath,&statbuf) == 0) {
  271.         errno = EEXIST;        /* Stat worked, so it already exists */
  272.         return -1;
  273.     }
  274.  
  275.     /* If stat fails for a reason other than non-existence, return error */
  276.     if (errno != ENOENT) return -1; 
  277.  
  278.     switch (cpid = fork()) {
  279.  
  280.     case -1:            /* Error in fork() */
  281.         return(-1);        /* Errno is set already */
  282.  
  283.     case 0:                /* Child process */
  284.         /*
  285.          * Cheap hack to set mode of new directory.  Since this
  286.          * child process is going away anyway, we zap its umask.
  287.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  288.          * directory.  Does anybody care?
  289.          */
  290.         status = umask(0);    /* Get current umask */
  291.         status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  292.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  293.         _exit(-1);        /* Can't exec /bin/mkdir */
  294.     
  295.     default:            /* Parent process */
  296.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  297.     }
  298.  
  299.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  300.         errno = EIO;        /* We don't know why, but */
  301.         return -1;        /* /bin/mkdir failed */
  302.     }
  303.  
  304.     return 0;
  305. }
  306. int
  307. rmdir(dpath)
  308.     char *dpath;
  309. {
  310.     int cpid, status;
  311.     struct stat statbuf;
  312.     extern int errno;
  313.  
  314.     if (stat(dpath,&statbuf) != 0) {
  315.         /* Stat just set errno.  We don't have to */
  316.         return -1;
  317.     }
  318.  
  319.     switch (cpid = fork()) {
  320.  
  321.     case -1:            /* Error in fork() */
  322.         return(-1);        /* Errno is set already */
  323.  
  324.     case 0:                /* Child process */
  325.         execl("/bin/rmdir", "rmdir", dpath, (char *)0);
  326.         _exit(-1);        /* Can't exec /bin/mkdir */
  327.     
  328.     default:            /* Parent process */
  329.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  330.     }
  331.  
  332.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  333.         errno = EIO;        /* We don't know why, but */
  334.         return -1;        /* /bin/mkdir failed */
  335.     }
  336.  
  337.     return 0;
  338. }
  339. #endif
  340.  
  341. #ifdef WANT_STRING
  342. /*
  343.  * Translate V7 style into Sys V style.
  344.  */
  345. #include <string.h>
  346. #ifndef __MSDOS__
  347. #include <memory.h>
  348. #endif
  349.  
  350. char *
  351. index (s, c)
  352.     char *s;
  353.     int c;
  354. {
  355.     return (strchr (s, c));
  356. }
  357.  
  358. char *
  359. rindex(s,c)
  360. char *s;
  361. int c;
  362. {
  363.     return strrchr(s,c);
  364. }
  365.  
  366. void
  367. bcopy (s1, s2, n)
  368.     char *s1, *s2;
  369.     int n;
  370. {
  371.     (void) memcpy (s2, s1, n);
  372. }
  373.  
  374. void
  375. bzero (s1, n)
  376.     char *s1;
  377.     int n;
  378. {
  379.     (void) memset(s1, 0, n);
  380. }
  381.  
  382. int
  383. bcmp(s1, s2, n)
  384.     char    *s1, *s2;
  385.     int    n;
  386. {
  387.     return memcmp(s1, s2, n);
  388. }
  389. #endif
  390.  
  391. #ifdef WANT_BZERO
  392. /* Minix has bcopy but not bzero, and no memset.  Thanks, Andy. */
  393. void
  394. bzero (s1, n)
  395.     register char *s1;
  396.     register int n;
  397. {
  398.     while (n--) *s1++ = '\0';
  399. }
  400.  
  401. /* It also has no bcmp() */
  402. int
  403. bcmp (s1, s2, n) 
  404.     register char *s1,*s2;
  405.     register int n;
  406. {
  407.     for ( ; n-- ; ++s1, ++s2) {
  408.         if (*s1 != *s2) return *s1 - *s2;
  409.     }
  410.     return 0;
  411. }
  412.  
  413. /*
  414.  * Groan, Minix doesn't have execlp either!
  415.  *
  416.  * execlp(file,arg0,arg1...argn,(char *)NULL)
  417.  * exec a program, automatically searching for the program through
  418.  * all the directories on the PATH.
  419.  *
  420.  * This version is naive about variable argument lists, it assumes
  421.  * a straightforward C calling sequence.  If your system has odd stacks
  422.  * *and* doesn't have execlp, YOU get to fix it.
  423.  */
  424. int
  425. execlp(filename, arg0)
  426.     char *filename, *arg0;
  427. {
  428.     register char *p, *path;    
  429.     register char *fnbuffer;
  430.     char **argstart = &arg0;
  431.     struct stat statbuf;
  432.     extern char **environ;
  433.     extern int errno;
  434.     extern char *malloc(), *getenv(), *index();
  435.  
  436.     if ((p = getenv("PATH")) == NULL) {
  437.         /* couldn't find path variable -- try to exec given filename */
  438.         return execve(filename, argstart, environ);
  439.     }
  440.  
  441.     /*
  442.      * make a place to build the filename.  We malloc larger than we
  443.      * need, but we know it will fit in this.
  444.      */
  445.     fnbuffer = malloc( strlen(p) + 1 + strlen(filename) );
  446.     if (fnbuffer == NULL) {
  447.         errno = ENOMEM;
  448.         return -1;
  449.     }
  450.  
  451.     /*
  452.      * try each component of the path to see if the file's there
  453.      * and executable.
  454.      */
  455.     for (path = p ; path ; path = p) {
  456.         /* construct full path name to try */
  457.         if ((p = index(path,':')) == NULL) {
  458.             strcpy(fnbuffer, path);
  459.         } else {
  460.             strncpy(fnbuffer, path, p-path);
  461.             fnbuffer[p-path] = '\0';
  462.             p++;        /* Skip : for next time */
  463.         }
  464.         if (strlen(fnbuffer) != 0)
  465.             strcat(fnbuffer,"/");
  466.         strcat(fnbuffer,filename);
  467.  
  468.         /* check to see if file is there and is a normal file */
  469.         if (stat(fnbuffer, &statbuf) < 0) {
  470.             if (errno == ENOENT)
  471.                 continue; /* file not there,keep on looking */
  472.             else
  473.                 goto fail; /* failed for some reason, return */
  474.         }
  475.         if ( (statbuf.st_mode & S_IFMT) != S_IFREG) continue;
  476.  
  477.         if (execve(fnbuffer, argstart, environ) < 0
  478.             && errno != ENOENT
  479.             && errno != ENOEXEC) {
  480.             /* failed, for some other reason besides "file
  481.              * not found" or "not a.out format"
  482.              */
  483.             goto fail;
  484.         }
  485.  
  486.         /*
  487.          * If we got error ENOEXEC, the file is executable but is
  488.          * not an object file.  Try to execute it as a shell script,
  489.          * returning error if we can't execute /bin/sh.
  490.          *
  491.          * FIXME, this code is broken in several ways.  Shell
  492.          * scripts should not in general be executed by the user's
  493.          * SHELL variable program.  On more mature systems, the
  494.          * script can specify with #!/bin/whatever.  Also, this
  495.          * code clobbers argstart[-1] if the exec of the shell
  496.          * fails.
  497.          */
  498.         if (errno == ENOEXEC) {
  499.             char *shell;
  500.  
  501.             /* Try to execute command "sh arg0 arg1 ..." */
  502.             if ((shell = getenv("SHELL")) == NULL)
  503.                 shell = "/bin/sh";
  504.             argstart[-1] = shell;
  505.             argstart[0] = fnbuffer;
  506.             execve(shell, &argstart[-1], environ);
  507.             goto fail;    /* Exec didn't work */
  508.         }
  509.  
  510.         /* 
  511.          * If we succeeded, the execve() doesn't return, so we
  512.          * can only be here is if the file hasn't been found yet.
  513.          * Try the next place on the path.
  514.          */
  515.     }
  516.  
  517.     /* all attempts failed to locate the file.  Give up. */
  518.     errno = ENOENT;
  519.  
  520. fail:
  521.     free(fnbuffer);
  522.     return -1;
  523. }
  524. #endif
  525.  
  526.  
  527. #ifdef EMUL_OPEN3
  528. #include "open3.h"
  529. /*
  530.  * open3 -- routine to emulate the 3-argument open system
  531.  * call that is present in most modern Unix systems.
  532.  * This version attempts to support all the flag bits except for O_NDELAY
  533.  * and O_APPEND, which are silently ignored.  The emulation is not as efficient
  534.  * as the real thing (at worst, 4 system calls instead of one), but there's
  535.  * not much I can do about that.
  536.  *
  537.  * Written 6/10/87 by rmtodd@uokmax
  538.  *
  539.  * open3(path, flag, mode)
  540.  * Attempts to open the file specified by
  541.  * the given pathname.  The following flag bits (#defined in tar.h)
  542.  * specify options to the routine:
  543.  *    O_RDONLY    file open for read only
  544.  *    O_WRONLY    file open for write only
  545.  *    O_RDWR        file open for both read & write
  546.  * (Needless to say, you should only specify one of the above).
  547.  *     O_CREAT        file is created with specified mode if it needs to be.
  548.  *    O_TRUNC        if file exists, it is truncated to 0 bytes
  549.  *    O_EXCL        used with O_CREAT--routine returns error if file exists
  550.  * Function returns file descriptor if successful, -1 and errno if not.
  551.  */
  552.  
  553. /*
  554.  * array to give arguments to access for various modes
  555.  * FIXME, this table depends on the specific integer values of O_XXX,
  556.  * and also contains integers (args to 'access') that should be #define's.
  557.  */
  558. static int modes[] =
  559.     {
  560.         04, /* O_RDONLY */
  561.         02, /* O_WRONLY */
  562.         06, /* O_RDWR */
  563.         06, /* invalid but we'd better cope -- O_WRONLY+O_RDWR */
  564.     };
  565.  
  566. /* Shut off the automatic emulation of open(), we'll need it. */
  567. #undef open
  568.  
  569. int
  570. open3(path, flags, mode)
  571. char *path;
  572. int flags, mode;
  573. {
  574.     extern int errno;
  575.     int exists = 1;
  576.     int call_creat = 0;
  577.     int fd;
  578.     /*
  579.      * We actually do the work by calling the open() or creat() system
  580.      * call, depending on the flags.  Call_creat is true if we will use 
  581.      * creat(), false if we will use open().
  582.      */
  583.  
  584.     /*
  585.      * See if the file exists and is accessible in the requested mode. 
  586.      *
  587.      * Strictly speaking we shouldn't be using access, since access checks
  588.      * against real uid, and the open call should check against euid.
  589.      * Most cases real uid == euid, so it won't matter.   FIXME.
  590.      * FIXME, the construction "flags & 3" and the modes table depends
  591.      * on the specific integer values of the O_XXX #define's.  Foo!
  592.      */
  593.     if (access(path,modes[flags & 3]) < 0) {
  594.         if (errno == ENOENT) {
  595.             /* the file does not exist */
  596.             exists = 0;
  597.         } else {
  598.             /* probably permission violation */
  599.             if (flags & O_EXCL) {
  600.                 /* Oops, the file exists, we didn't want it. */
  601.                 /* No matter what the error, claim EEXIST. */
  602.                 errno = EEXIST;
  603.             }
  604.             return -1;
  605.         }
  606.     }
  607.  
  608.     /* if we have the O_CREAT bit set, check for O_EXCL */
  609.     if (flags & O_CREAT) {
  610.         if ((flags & O_EXCL) && exists) {
  611.             /* Oops, the file exists and we didn't want it to. */
  612.             errno = EEXIST;
  613.             return -1;
  614.         }
  615.         /*
  616.          * If the file doesn't exist, be sure to call creat() so that
  617.          * it will be created with the proper mode.
  618.          */
  619.         if (!exists) call_creat = 1;
  620.     } else {
  621.         /* If O_CREAT isn't set and the file doesn't exist, error. */
  622.         if (!exists) {
  623.             errno = ENOENT;
  624.             return -1;
  625.         }
  626.     }
  627.  
  628.     /*
  629.      * If the O_TRUNC flag is set and the file exists, we want to call
  630.      * creat() anyway, since creat() guarantees that the file will be
  631.      * truncated and open()-for-writing doesn't.
  632.      * (If the file doesn't exist, we're calling creat() anyway and the
  633.      * file will be created with zero length.)
  634.      */
  635.     if ((flags & O_TRUNC) && exists) call_creat = 1;
  636.     /* actually do the call */
  637.     if (call_creat) {
  638.         /*
  639.          * call creat.  May have to close and reopen the file if we
  640.          * want O_RDONLY or O_RDWR access -- creat() only gives
  641.          * O_WRONLY.
  642.          */
  643.         fd = creat(path,mode);
  644.         if (fd < 0 || (flags & O_WRONLY)) return fd;
  645.         if (close(fd) < 0) return -1;
  646.         /* Fall out to reopen the file we've created */
  647.     }
  648.  
  649.     /*
  650.      * calling old open, we strip most of the new flags just in case.
  651.      */
  652.     return open(path, flags & (O_RDONLY|O_WRONLY|O_RDWR|O_BINARY));
  653. }
  654. #endif
  655.  
  656. #ifdef    WANT_MKNOD
  657. #ifdef __MSDOS__
  658. typedef int dev_t;
  659. #endif
  660. /* Fake mknod by complaining */
  661. int
  662. mknod(path, mode, dev)
  663.     char        *path;
  664.     unsigned short    mode;
  665.     dev_t        dev;
  666. {
  667.     extern int    errno;
  668.     int        fd;
  669.     
  670.     errno = ENXIO;        /* No such device or address */
  671.     return -1;        /* Just give an error */
  672. }
  673.  
  674. /* Fake links by copying */
  675. int
  676. link(path1, path2)
  677.     char        *path1;
  678.     char        *path2;
  679. {
  680.     char    buf[256];
  681.     int    ifd, ofd;
  682.     int    nrbytes;
  683.     int    nwbytes;
  684.  
  685.     fprintf(stderr, "%s: %s: cannot link to %s, copying instead\n",
  686.         tar, path1, path2);
  687.     if ((ifd = open(path1, O_RDONLY|O_BINARY)) < 0)
  688.         return -1;
  689.     if ((ofd = creat(path2, 0666)) < 0)
  690.         return -1;
  691.     setmode(ofd, O_BINARY);
  692.     while ((nrbytes = read(ifd, buf, sizeof(buf))) > 0) {
  693.         if ((nwbytes = write(ofd, buf, nrbytes)) != nrbytes) {
  694.             nrbytes = -1;
  695.             break;
  696.         }
  697.     }
  698.     /* Note use of "|" rather than "||" below: we want to close
  699.      * the files even if an error occurs.
  700.      */
  701.     if ((nrbytes < 0) | (0 != close(ifd)) | (0 != close(ofd))) {
  702.         unlink(path2);
  703.         return -1;
  704.     }
  705.     return 0;
  706. }
  707.  
  708. /* everyone owns everything on MS-DOS (or is it no one owns anything?) */
  709. int
  710. chown(path, uid, gid)
  711.     char    *path;
  712.     int    uid;
  713.     int    gid;
  714. {
  715.     return 0;
  716. }
  717.  
  718. int
  719. geteuid()
  720. {
  721.     return 0;
  722. }
  723. #endif    /* WANT_MKNOD */
  724.  
  725. #ifdef WANT_UTILS
  726. /* Stash argv[0] here so panic will know what the program is called */
  727. char *myname = 0;
  728.  
  729. void
  730. panic(s)
  731. char *s;
  732. {
  733.     if(myname)
  734.         fprintf(stderr,"%s:",myname);
  735.     fprintf(stderr,s);
  736.     putc('\n',stderr);
  737.     exit(12);
  738. }
  739.  
  740.  
  741. char *
  742. ck_malloc(size)
  743. size_t size;
  744. {
  745.     char *ret;
  746.     char *malloc();
  747.  
  748.     if(!size)
  749.         size++;
  750.     ret=malloc(size);
  751.     if(ret==0)
  752.         panic("Couldn't allocate memory");
  753.     return ret;
  754. }
  755.  
  756. char *
  757. ck_realloc(ptr,size)
  758. char *ptr;
  759. size_t size;
  760. {
  761.     char *ret;
  762.     char *realloc();
  763.  
  764.     if(!ptr)
  765.         ret=ck_malloc(size);
  766.     else
  767.         ret=realloc(ptr,size);
  768.     if(ret==0)
  769.         panic("Couldn't re-allocate memory");
  770.     return ret;
  771. }
  772.  
  773. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  774.    nor do we care, as long as it doesn't mind being aligned on a char boundry.
  775.  */
  776.  
  777. struct buffer {
  778.     int    allocated;
  779.     int    length;
  780.     char    *b;
  781. };
  782.  
  783. #define MIN_ALLOCATE 50
  784.  
  785. char *
  786. init_buffer()
  787. {
  788.     struct buffer *b;
  789.  
  790.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  791.     b->allocated=MIN_ALLOCATE;
  792.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  793.     b->length=0;
  794.     return (char *)b;
  795. }
  796.  
  797. void
  798. flush_buffer(bb)
  799. char *bb;
  800. {
  801.     struct buffer *b;
  802.  
  803.     b=(struct buffer *)bb;
  804.     free(b->b);
  805.     b->b=0;
  806.     b->allocated=0;
  807.     b->length=0;
  808.     free((void *)b);
  809. }
  810.  
  811. void
  812. add_buffer(bb,p,n)
  813. char *bb;
  814. char *p;
  815. int n;
  816. {
  817.     struct buffer *b;
  818.  
  819.     b=(struct buffer *)bb;
  820.     if(b->length+n>b->allocated) {
  821.         b->allocated=b->length+n+MIN_ALLOCATE;
  822.         b->b=(char *)ck_realloc(b->b,b->allocated);
  823.     }
  824.     bcopy(p,b->b+b->length,n);
  825.     b->length+=n;
  826. }
  827.  
  828. char *
  829. get_buffer(bb)
  830. char *bb;
  831. {
  832.     struct buffer *b;
  833.  
  834.     b=(struct buffer *)bb;
  835.     return b->b;
  836. }
  837.  
  838. char *
  839. merge_sort(list,n,off,cmp)
  840. char *list;
  841. int (*cmp)();
  842. unsigned n;
  843. int off;
  844. {
  845.     char *ret;
  846.  
  847.     char *alist,*blist;
  848.     unsigned alength,blength;
  849.  
  850.     char *tptr;
  851.     int tmp;
  852.     char **prev;
  853. #define NEXTOF(ptr)    (* ((char **)(((char *)(ptr))+off) ) )
  854.     if(n==1)
  855.         return list;
  856.     if(n==2) {
  857.         if((*cmp)(list,NEXTOF(list))>0) {
  858.             ret=NEXTOF(list);
  859.             NEXTOF(ret)=list;
  860.             NEXTOF(list)=0;
  861.             return ret;
  862.         }
  863.         return list;
  864.     }
  865.     alist=list;
  866.     alength=(n+1)/2;
  867.     blength=n/2;
  868.     for(tptr=list,tmp=(n-1)/2;tmp;tptr=NEXTOF(tptr),tmp--)
  869.         ;
  870.     blist=NEXTOF(tptr);
  871.     NEXTOF(tptr)=0;
  872.  
  873.     alist=merge_sort(alist,alength,off,cmp);
  874.     blist=merge_sort(blist,blength,off,cmp);
  875.     prev = &ret;
  876.     for(;alist && blist;) {
  877.         if((*cmp)(alist,blist)<0) {
  878.             tptr=NEXTOF(alist);
  879.             *prev = alist;
  880.             prev = &(NEXTOF(alist));
  881.             alist=tptr;
  882.         } else {
  883.             tptr=NEXTOF(blist);
  884.             *prev = blist;
  885.             prev = &(NEXTOF(blist));
  886.             blist=tptr;
  887.         }
  888.     }
  889.     if(alist)
  890.         *prev = alist;
  891.     else
  892.         *prev = blist;
  893.  
  894.     return ret;
  895. }
  896.  
  897. void
  898. ck_close(fd)
  899. int fd;
  900. {
  901.     if(close(fd)<0) {
  902.         msg_perror("can't close a file #%d",fd);
  903.         exit(EX_SYSTEM);
  904.     }
  905. }
  906.  
  907. #include <ctype.h>
  908.  
  909. /* Quote_copy_string is like quote_string, but instead of modifying the
  910.    string in place, it malloc-s a copy  of the string, and returns that.
  911.    If the string does not have to be quoted, it returns the NULL string.
  912.    The allocated copy can, of course, be freed with free() after the
  913.    caller is done with it.
  914.  */
  915. char *
  916. quote_copy_string(string)
  917. char *string;
  918. {
  919.     char    *from_here;
  920.     char    *to_there = 0;
  921.     char    *copy_buf = 0;
  922.     int    c;
  923.     int    copying = 0;
  924.  
  925.     from_here=string;
  926.     while(*from_here) {
  927.         c= *from_here++;
  928.         if(c=='\\') {
  929.             if(!copying) {
  930.                 int n;
  931.  
  932.                 n=(from_here-string)-1;
  933.                 copying++;
  934.                 copy_buf=(char *)malloc(n+5+strlen(from_here)*4);
  935.                 if(!copy_buf)
  936.                     return 0;
  937.                 bcopy(string,copy_buf,n);
  938.                 to_there=copy_buf+n;
  939.             }
  940.             *to_there++='\\';
  941.             *to_there++='\\';
  942.         } else if(isprint(c)) {
  943.             if(copying)
  944.                 *to_there++= c;
  945.         } else {
  946.             if(!copying) {
  947.                 int    n;
  948.  
  949.                 n=(from_here-string)-1;
  950.                 copying++;
  951.                 copy_buf=(char *)malloc(n+5+strlen(from_here)*4);
  952.                 if(!copy_buf)
  953.                     return 0;
  954.                 bcopy(string,copy_buf,n);
  955.                 to_there=copy_buf+n;
  956.             }
  957.             *to_there++='\\';
  958.             if(c=='\n') *to_there++='n';
  959.             else if(c=='\t') *to_there++='t';
  960.             else if(c=='\f') *to_there++='f';
  961.             else if(c=='\b') *to_there++='b';
  962.             else if(c=='\r') *to_there++='r';
  963.             else if(c=='\177') *to_there++='?';
  964.             else {
  965.                 to_there[0]=(c>>6)+'0';
  966.                 to_there[1]=((c>>3)&07)+'0';
  967.                 to_there[2]=(c&07)+'0';
  968.                 to_there+=3;
  969.             }
  970.         }
  971.     }
  972.     if(copying) {
  973.         *to_there='\0';
  974.         return copy_buf;
  975.     }
  976.     return (char *)0;
  977. }
  978.  
  979.  
  980. /* Un_quote_string takes a quoted c-string (like those produced by
  981.    quote_string or quote_copy_string and turns it back into the
  982.    un-quoted original.  This is done in place.
  983.  */
  984.  
  985. /* There is no un-quote-copy-string.  Write it yourself */
  986.  
  987. char *un_quote_string(string)
  988. char *string;
  989. {
  990.     char *ret;
  991.     char *from_here;
  992.     char *to_there;
  993.     int    tmp;
  994.  
  995.     ret=string;
  996.     to_there=string;
  997.     from_here=string;
  998.     while(*from_here) {
  999.         if(*from_here!='\\') {
  1000.             if(from_here!=to_there)
  1001.                 *to_there++= *from_here++;
  1002.             else
  1003.                 from_here++,to_there++;
  1004.             continue;
  1005.         }
  1006.         switch(*++from_here) {
  1007.         case '\\':
  1008.             *to_there++= *from_here++;
  1009.             break;
  1010.         case 'n':
  1011.             *to_there++= '\n';
  1012.             from_here++;
  1013.             break;
  1014.         case 't':
  1015.             *to_there++= '\t';
  1016.             from_here++;
  1017.             break;
  1018.         case 'f':
  1019.             *to_there++= '\f';
  1020.             from_here++;
  1021.             break;
  1022.         case 'b':
  1023.             *to_there++= '\b';
  1024.             from_here++;
  1025.             break;
  1026.         case 'r':
  1027.             *to_there++= '\r';
  1028.             from_here++;
  1029.             break;
  1030.         case '?':
  1031.             *to_there++= 0177;
  1032.             from_here++;
  1033.             break;
  1034.         case '0':
  1035.         case '1':
  1036.         case '2':
  1037.         case '3':
  1038.         case '4':
  1039.         case '5':
  1040.         case '6':
  1041.         case '7':
  1042.             tmp= *from_here - '0';
  1043.             from_here++;
  1044.             if(*from_here<'0' || *from_here>'7') {
  1045.                 *to_there++= tmp;
  1046.                 break;
  1047.             }
  1048.             tmp= tmp*8 + *from_here-'0';
  1049.             from_here++;
  1050.             if(*from_here<'0' || *from_here>'7') {
  1051.                 *to_there++= tmp;
  1052.                 break;
  1053.             }
  1054.             tmp=tmp*8 + *from_here-'0';
  1055.             from_here++;
  1056.             *to_there=tmp;
  1057.             break;
  1058.         default:
  1059.             ret=0;
  1060.             *to_there++='\\';
  1061.             *to_there++= *from_here++;
  1062.             break;
  1063.         }
  1064.     }
  1065.     if(*to_there)
  1066.         *to_there++='\0';
  1067.     return ret;
  1068. }
  1069. #endif
  1070.  
  1071. #ifdef WANT_CK_PIPE
  1072. void ck_pipe(pipes)
  1073. int *pipes;
  1074. {
  1075.     if(pipe(pipes)<0) {
  1076.         msg_perror("can't open a pipe");
  1077.         exit(EX_SYSTEM);
  1078.     }
  1079. }
  1080.  
  1081. #endif
  1082.  
  1083. #ifdef WANT_GETWD
  1084. char *
  1085. getwd(path)
  1086. char *path;
  1087. {
  1088.     FILE *fp;
  1089.     FILE *popen();
  1090.  
  1091.     fp=popen("pwd","r");
  1092.     if(!fp)
  1093.         return 0;
  1094.     if(!fgets(path,100,fp))
  1095.         return 0;
  1096.     if(!pclose(fp))
  1097.         return 0;
  1098.     return path;
  1099. }
  1100. #endif /* WANT_CK_PIPE */
  1101.  
  1102.  
  1103. #ifdef WANT_STRSTR
  1104.  
  1105. /*
  1106.  * strstr - find first occurrence of wanted in s
  1107.  */
  1108.  
  1109. char *                /* found string, or NULL if none */
  1110. strstr(s, wanted)
  1111. char *s;
  1112. char *wanted;
  1113. {
  1114.     register char *scan;
  1115.     register size_t len;
  1116.     register char firstc;
  1117.     extern int strcmp();
  1118.  
  1119.     if (*wanted == '\0')
  1120.       return (char *)0;
  1121.     /*
  1122.      * The odd placement of the two tests is so "" is findable.
  1123.      * Also, we inline the first char for speed.
  1124.      * The ++ on scan has been moved down for optimization.
  1125.      */
  1126.     firstc = *wanted;
  1127.     len = strlen(wanted);
  1128.     for (scan = s; *scan != firstc || strncmp(scan, wanted, len) != 0; )
  1129.         if (*scan++ == '\0')
  1130.             return (char *)0;
  1131.     return scan;
  1132. }
  1133. #endif
  1134.  
  1135. #ifdef WANT_FTRUNCATE
  1136.  
  1137. #ifdef F_FREESP
  1138. /* code courtesy of William Kucharski */
  1139.  
  1140. int
  1141. ftruncate(fd, length)
  1142. int fd;                       /* file descriptor */
  1143. off_t length;         /* length to set file to */
  1144. {
  1145.     struct flock fl;
  1146.  
  1147.     fl.l_whence = 0;
  1148.     fl.l_len = 0;
  1149.     fl.l_start = length;
  1150.     fl.l_type = F_WRLCK;    /* write lock on file space */
  1151.  
  1152.     /*
  1153.      * This relies on the UNDOCUMENTED F_FREESP argument to
  1154.      * fcntl(2), which truncates the file so that it ends at the
  1155.      * position indicated by fl.l_start.
  1156.      *
  1157.      * Will minor miracles never cease?
  1158.      */
  1159.  
  1160.     if (fcntl(fd, F_FREESP, &fl) < 0)
  1161.         return -1;
  1162.  
  1163.     return 0;
  1164. }
  1165.  
  1166.  
  1167. #else
  1168. int
  1169. ftruncate(fd, length)
  1170. int fd;
  1171. off_t length;
  1172. {
  1173.     errno = EIO;
  1174.     return -1;
  1175. }
  1176. #endif
  1177.  
  1178. #endif
  1179.  
  1180.  
  1181.  
  1182.  
  1183. extern FILE *msg_file;
  1184.  
  1185. #ifdef STDC_MSG
  1186. #include <stdarg.h>
  1187.  
  1188. void
  1189. msg(char *str,...)
  1190. {
  1191.     va_list args;
  1192.  
  1193.     va_start(args,str);
  1194.     fflush(msg_file);
  1195.     fprintf(stderr,"%s: ",tar);
  1196.     if(f_sayblock)
  1197.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1198.     vfprintf(stderr,str,args);
  1199.     va_end(args);
  1200.     putc('\n',stderr);
  1201.     fflush(stderr);
  1202. }
  1203.  
  1204. void
  1205. msg_perror(char *str,...)
  1206. {
  1207.     va_list args;
  1208.     int save_e;
  1209.     extern int errno;
  1210.  
  1211.     save_e=errno;
  1212.     fflush(msg_file);
  1213.     fprintf(stderr,"%s: ",tar);
  1214.     if(f_sayblock)
  1215.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1216.     va_start(args,str);
  1217.     vfprintf(stderr,str,args);
  1218.     va_end(args);
  1219.     errno=save_e;
  1220.     perror(" ");
  1221.     fflush(stderr);
  1222. }
  1223. #endif
  1224.  
  1225. #ifdef VARARGS_MSG
  1226. #include <varargs.h>
  1227. void msg(str,va_alist)
  1228. char *str;
  1229. va_dcl
  1230. {
  1231.     va_list args;
  1232.  
  1233.     fflush(msg_file);
  1234.     fprintf(stderr,"%s: ",tar);
  1235.     if(f_sayblock)
  1236.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1237.     va_start(args);
  1238.     vfprintf(stderr,str,args);
  1239.     va_end(args);
  1240.     putc('\n',stderr);
  1241.     fflush(stderr);
  1242. }
  1243.  
  1244. void msg_perror(str,va_alist)
  1245. char *str;
  1246. va_dcl
  1247. {
  1248.     va_list args;
  1249.     int save_e;
  1250.     extern int errno;
  1251.  
  1252.     save_e=errno;
  1253.     fflush(msg_file);
  1254.     fprintf(stderr,"%s: ",tar);
  1255.     if(f_sayblock)
  1256.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1257.     va_start(args);
  1258.     vfprintf(stderr,str,args);
  1259.     va_end(args);
  1260.     errno=save_e;
  1261.     perror(" ");
  1262.     fflush(stderr);
  1263. }
  1264. #endif
  1265.  
  1266. #ifdef DOPRNT_MSG
  1267. void msg(str,args)
  1268. char *str;
  1269. int args;
  1270. {
  1271.     fflush(msg_file);
  1272.     fprintf(stderr,"%s: ",tar);
  1273.     if(f_sayblock)
  1274.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1275.     _doprnt(str, &args, stderr);
  1276.     putc('\n',stderr);
  1277.     fflush(stderr);
  1278. }
  1279.  
  1280. void msg_perror(str,args)
  1281. char *str;
  1282. {
  1283.     int save_e;
  1284.     extern int errno;
  1285.  
  1286.     save_e=errno;
  1287.     fflush(msg_file);
  1288.     fprintf(stderr,"%s: ",tar);
  1289.     if(f_sayblock)
  1290.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1291.     _doprnt(str, &args, stderr);
  1292.     errno=save_e;
  1293.     perror(" ");
  1294.     fflush(stderr);
  1295. }
  1296.  
  1297. #endif
  1298. #ifdef LOSING_MSG
  1299. void msg(str,a1,a2,a3,a4,a5,a6)
  1300. char *str;
  1301. {
  1302.     fflush(msg_file);
  1303.     fprintf(stderr,"%s: ",tar);
  1304.     if(f_sayblock)
  1305.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1306.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1307.     putc('\n',stderr);
  1308.     fflush(stderr);
  1309. }
  1310.  
  1311. void msg_perror(str,a1,a2,a3,a4,a5,a6)
  1312. char *str;
  1313. {
  1314.     int save_e;
  1315.     extern int errno;
  1316.  
  1317.     save_e=errno;
  1318.     fflush(msg_file);
  1319.     fprintf(stderr,"%s: ",tar);
  1320.     if(f_sayblock)
  1321.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1322.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1323.     fprintf(stderr,": ");
  1324.     errno=save_e;
  1325.     perror(" ");
  1326. }
  1327.  
  1328. #endif
  1329.